package com.example.service.impl;

import com.example.mapper.SpecificationMapper;
import com.example.mapper.SpecificationoptionMapper;
import com.example.pojo.Specification;
import com.example.pojo.SpecificationExample;
import com.example.pojo.SpecificationOption;
import com.example.pojo.SpecificationOptionExample;
import com.example.req.SpecificationGroup;
import com.example.req.SpecificationReq;
import com.example.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private SpecificationoptionMapper specificationoptionMapper;

    @Override
    public List<Specification> queryByCondition(SpecificationReq specificationReq) {
        SpecificationExample example = new SpecificationExample();
        SpecificationExample.Criteria criteria = example.createCriteria();
        if (specificationReq != null) {
            if (!ObjectUtils.isEmpty(specificationReq.getSpecName())){
                criteria.andSpecNameLike( "%" + specificationReq.getSpecName() + "%");
            }
        }
        return specificationMapper.selectByExample(example);
    }

    @Override
    public List<SpecificationOption> querySpecificationOptionBySpecId(Long id) {
        SpecificationOptionExample example = new SpecificationOptionExample();
        SpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        return specificationoptionMapper.selectByExample(example);
    }

    @Override
    @Transactional
    public int saveOrUpdate(SpecificationGroup specificationGroup) {
        if (specificationGroup.getSpecification().getId() != null){
            //更新规格表
            int update = specificationMapper.updateByPrimaryKey(specificationGroup.getSpecification());
            //更新规格详情表（先删除后插入）
            SpecificationOptionExample example = new SpecificationOptionExample();
            SpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(specificationGroup.getSpecification().getId());
            int delete = specificationoptionMapper.deleteByExample(example);

            for (SpecificationOption option : specificationGroup.getSpecificationOptionList()) {
                option.setSpecId(specificationGroup.getSpecification().getId());
                int insert = specificationoptionMapper.insert(option);
            }

            return update;
        }else {
            int insert = specificationMapper.insert(specificationGroup.getSpecification());
//            System.out.println("回填的id：" + specificationGroup.getSpecification().getId());

            for (SpecificationOption option : specificationGroup.getSpecificationOptionList()) {
                option.setSpecId(specificationGroup.getSpecification().getId());
                int insert1 = specificationoptionMapper.insert(option);
            }

            return insert;
        }
    }

    @Override
    @Transactional
    public int deleteById(Long id) {

        SpecificationOptionExample example = new SpecificationOptionExample();
        SpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        int delete1 = specificationoptionMapper.deleteByExample(example);
        int delete = specificationMapper.deleteByPrimaryKey(id);

        return delete + delete1;
    }
}
